<html>
<head><meta charset="utf-8"><title>mutable graph traversal · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html">mutable graph traversal</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214731154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20graph%20traversal/near/214731154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Mandelkow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html#214731154">(Oct 27 2020 at 17:05)</a>:</h4>
<p>I've been creating bindings for an imgui node editing lib and am porting the last example and finally hit an issue with graphs, of course I stayed away from pointers but I fail to express a mutable traversal of the node graph :/ I created a minimal example: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=4e4b74932e4ed7f0c097e10160df3384">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=4e4b74932e4ed7f0c097e10160df3384</a>  ...pointers to blog posts or other solutions which ideally don't require dealing with a lot of indices or external crates would be much appreciated :D</p>



<a name="214731710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20graph%20traversal/near/214731710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html#214731710">(Oct 27 2020 at 17:08)</a>:</h4>
<p>You can fix that problem by moving out the <code>self.links</code> borrow into its own variable, so that the closure doesnt borrow <code>self</code> entirely:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Graph</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">get_inputs_of</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">output</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="nc">Node</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">impl</span><span class="w"> </span><span class="n">std</span>::<span class="n">iter</span>::<span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">Node</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">links</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="n">links</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">nodes</span><span class="p">.</span><span class="n">iter_mut</span><span class="p">().</span><span class="n">filter</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="o">|</span><span class="n">input</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">links</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">any</span><span class="p">(</span><span class="o">|</span><span class="n">link</span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">link</span><span class="p">.</span><span class="n">start</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">input</span><span class="p">.</span><span class="n">output</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="p">(</span><span class="n">link</span><span class="p">.</span><span class="n">end</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">output</span><span class="p">.</span><span class="n">input</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="214731771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20graph%20traversal/near/214731771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html#214731771">(Oct 27 2020 at 17:09)</a>:</h4>
<p>Thoug you will run into more problems after that one</p>



<a name="214733038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20graph%20traversal/near/214733038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Mandelkow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html#214733038">(Oct 27 2020 at 17:18)</a>:</h4>
<p>hm yeah.. my issue is also that I don't really understand why the compiler does not approve what I did :D like is the concept of recursive mutable graph traversal just not possible to express without tricking the compiler?</p>



<a name="214734128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20graph%20traversal/near/214734128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html#214734128">(Oct 27 2020 at 17:25)</a>:</h4>
<p>The first problem that I showed you a fix for was due to the closure borrowing <code>self</code> while you also borrowed <code>self.nodes</code> mutably. Those borrows aren't disjoint and go against the borrow checker rules. The other problems you are running into afterwards are due to similar reasons. You are trying to mutably borrow things more than once at a time.</p>



<a name="214736298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20graph%20traversal/near/214736298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Mandelkow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html#214736298">(Oct 27 2020 at 17:39)</a>:</h4>
<p>yes cause what I do is basically get an iterator based on self and then want to mutate self based on this iterator which seems fine to me but not to rustc, I don't invalidate the first iterator but rust maybe thinks that I could....</p>



<a name="214737628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20graph%20traversal/near/214737628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Mandelkow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html#214737628">(Oct 27 2020 at 17:49)</a>:</h4>
<p>so users will have to do this for the lib to be of any use, petgraph offers no generic graph trait so the user would loose all the nice things of custom Node types and the other experimental lib is <a href="https://depth-first.com/articles/2020/02/17/graphs-in-rust-introducting-graphcore/">https://depth-first.com/articles/2020/02/17/graphs-in-rust-introducting-graphcore/</a> or I roll my own index based thing to trick the borrow checker sufficiently that it approves what I need to do</p>



<a name="214829802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/mutable%20graph%20traversal/near/214829802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Mandelkow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/mutable.20graph.20traversal.html#214829802">(Oct 28 2020 at 12:38)</a>:</h4>
<p>this is my solution <a href="https://github.com/benmkw/imnodes-rs/blob/master/src/graph.rs">https://github.com/benmkw/imnodes-rs/blob/master/src/graph.rs</a> for  <a href="https://github.com/benmkw/imnodes-rs/blob/master/imnodes-wgpu-examples/src/color_editor.rs">https://github.com/benmkw/imnodes-rs/blob/master/imnodes-wgpu-examples/src/color_editor.rs</a> so  as I understand I'm now one of those waiting for GAT and then giving it a try again ;)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>